package com.haixiaoke.saas.enterprise.apartment.controller;

import com.haixiaoke.saas.common.annotation.TenantScope;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.tenant.TenantContext;
import com.haixiaoke.saas.common.utils.poi.ExcelUtil;
import com.haixiaoke.saas.apartment.domain.ApartmentLabel;
import com.haixiaoke.saas.apartment.service.IApartmentLabelService;
import com.haixiaoke.saas.system.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 公寓标签Controller
 *
 * @author xiaolong
 * @date 2023-10-24
 */
@RestController
@RequestMapping("/enterprise/apartment/label")
public class EnterpriseApartmentLabelController extends BaseController {
    @Autowired
    private IApartmentLabelService apartmentLabelService;

    /**
     * 查询公寓标签列表
     */
    @PreAuthorize("@ss.hasPermi('business:label:list')")
    @GetMapping("/list")
    @TenantScope
    public TableDataInfo list(ApartmentLabel apartmentLabel) {
        startPage();
        apartmentLabel.setTenantId(TenantContext.getTenantId());
        List<ApartmentLabel> list = apartmentLabelService.selectApartmentLabelList(apartmentLabel);
        return getDataTable(list);
    }

    /**
     * 导出公寓标签列表
     */
    @PreAuthorize("@ss.hasPermi('business:label:export')")
    @PostMapping("/export")
    @TenantScope
    public void export(HttpServletResponse response, ApartmentLabel apartmentLabel) {
        apartmentLabel.setTenantId(TenantContext.getTenantId());
        List<ApartmentLabel> list = apartmentLabelService.selectApartmentLabelList(apartmentLabel);
        ExcelUtil<ApartmentLabel> util = new ExcelUtil<ApartmentLabel>(ApartmentLabel.class);
        util.exportExcel(response, list, "公寓标签数据");
    }

    /**
     * 获取公寓标签详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:label:query')")
    @GetMapping(value = "/{labelId}")
    public AjaxResult getInfo(@PathVariable("labelId") Long labelId) {
        return success(apartmentLabelService.selectApartmentLabelByLabelId(labelId));
    }

    /**
     * 新增公寓标签
     */
    @PreAuthorize("@ss.hasPermi('business:label:add')")
    @PostMapping
    @TenantScope
    public AjaxResult add(@RequestBody ApartmentLabel apartmentLabel) {
        apartmentLabel.setTenantId(TenantContext.getTenantId());
        return success(apartmentLabelService.insertApartmentLabel(apartmentLabel));
    }

    /**
     * 修改公寓标签
     */
    @PreAuthorize("@ss.hasPermi('business:label:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody ApartmentLabel apartmentLabel) {
        return toAjax(apartmentLabelService.updateApartmentLabel(apartmentLabel));
    }

    /**
     * 删除公寓标签
     */
    @PreAuthorize("@ss.hasPermi('business:label:remove')")
    @DeleteMapping("/{labelIds}")
    @TenantScope
    public AjaxResult remove(@PathVariable Long[] labelIds) {
        return toAjax(apartmentLabelService.deleteApartmentLabelByLabelIds(labelIds));
    }

    /**
     * 获取机构下的标签
     */
    @GetMapping(value = "/getAllInfo")
    @TenantScope
    public AjaxResult getAllInfo() {
        String tenantId = TenantContext.getTenantId();
        List<ApartmentLabel> list = apartmentLabelService.getAllInfo(tenantId);
        return success(list);
    }
}
